﻿var na_feeds = new Object();
var na_feeds_Indiviual = new Object();
var undecided = [];
var na_partyposition = new Object();
var Curr_ID = 1;
var TableID = "1k3ZqEnVxEWvsSLSf6s6h-WkNbfL0oPQa35xi-VM";
//var TableID = "1gndnRh87mGXtCTsAem-GFvNdaQ-XYG6N1QFzZMc";
var year = 2013;
var partyColor = {
    'MMA': 'bg-color-blue',
    'PPPS': 'bg-color-blueLight',
    'PMLQ': 'bg-color-blueDark',
    'IND': 'bg-color-green',
    'PPPP': 'bg-color-red',
    'PMLN': 'bg-color-greenDark',
    'NA': 'bg-color-red',
    'PTI': 'bg-color-yellow',
    'PMLJ': 'bg-color-orange',
    'PAT': 'bg-color-orangeDark',
    'PPP': 'bg-color-pink',
    'PSPP': 'bg-color-pinkDark',
    'PMLZ': 'bg-color-purple',
    'PML(QA)': 'bg-color-darken',
    'PML(FG)': 'bg-color-lighten',
    'MQM': 'bg-color-grayDark',
    'MQM(H)': 'bg-color-grayDark',
    'PKMAP': 'bg-color-magenta',
    'PML(N)': 'bg-color-teal',
    'JWP': 'bg-color-redLight',
    'BNP': 'bg-color-lighten',
    'ANP': 'bg-color-red',
    'PPP-S': 'bg-color-lighten',
    'PML': 'bg-color-lighten',
    'N/A': 'bg-color-lighten',
    'PMLF': 'bg-color-lighten',
    'NPP': 'bg-color-lighten',
    'BNPA': 'bg-color-lighten',
    'XXXX': 'bg-color-grayDark'
};

$(document).ready(function () {
    set_na_feeds();
    setInterval(function () {
        recheck();
    }, 10000);
});
var getPartyColor = function (propertyName) {
    return partyColor[propertyName];
};
function set_na_feeds() {
    if (!year) { return false; }

    var URL = "https://fusiontables.googleusercontent.com/fusiontables/api/query?sql=";
    
    
    var sSelect = "SELECT+*+FROM+" + TableID;
    var sWhere = "+where+Year=" + year + "+ORDER+BY+Year";    
    var sLimit = "+LIMIT+500";
    var sSQLURL = URL + sSelect + sWhere + sLimit;
    var querytail = "&jsonCallback=?";
    var jqxhr = jQuery.get(sSQLURL + querytail, set_na_feeds_handler, "jsonp");
}

function set_na_feeds_handler(data) {
    var rowNo = data.table.rows.length;
    if (rowNo <= 0) { return false; }
    var tt = 1;
    for (var i = 0; i < rowNo; i++) {
        na_feeds[data.table.rows[i][0]] = {
            "ID": data.table.rows[i][0],
            "Year": data.table.rows[i][1],
            "District": data.table.rows[i][3],
            "National Assembly Name": data.table.rows[i][2],
            "Registered Voters": data.table.rows[i][13],
            'Male Voters': data.table.rows[i][14],
            'Female Voters': data.table.rows[i][15],
            'Urbanisation': data.table.rows[i][16],
            "Turnout": data.table.rows[i][4],
            "Population": data.table.rows[i][5],
            "Gender Blnce Men": data.table.rows[i][17],
            "Gender Blnce Women": data.table.rows[i][18],
            "Urban": data.table.rows[i][19],
            "Rural": data.table.rows[i][20],
            "WATER": data.table.rows[i][21],
            "ELECTRICITY": data.table.rows[i][22],
            "PAKKA": data.table.rows[i][23],
            "Employment": data.table.rows[i][24],
            "Major Clans": data.table.rows[i][25],
            "Winning Party": data.table.rows[i][6],
            "Winning Candidate": data.table.rows[i][7],
            "Votes Polled for Winner": data.table.rows[i][8],
            "Runnerup Party": data.table.rows[i][9],
            "Runnerup Candidate": data.table.rows[i][10],
            "Votes Polled for Runnerup": data.table.rows[i][11],
            "Color Code": data.table.rows[i][12]
        };
    };
    addcards();
}

function set_partyposition(ID) {
    if (!ID) { return false; }

    var URL = "https://fusiontables.googleusercontent.com/fusiontables/api/query?sql=";    
    var sSelect = "SELECT+*+FROM+" + TableID;
    var sWhere = "+where+ID=" + ID + "+ORDER+BY+Year+DESC";
    var sLimit = "+LIMIT+500";
    var sSQLURL = URL + sSelect + sWhere + sLimit;
    var querytail = "&jsonCallback=?";
    var jqxhr = jQuery.get(sSQLURL + querytail, set_partyposition_handler, "jsonp");
}

function set_partyposition_handler(data) {
    var rowNo = data.table.rows.length;
    if (rowNo <= 0) { return false; }
    na_partyposition = new Object();
    var tt = 1;
    for (var i = 0; i < rowNo; i++) {
        na_partyposition[i] = {
            "ID": data.table.rows[i][0],
            "Year": data.table.rows[i][1],
            "District": data.table.rows[i][3],
            "National Assembly Name": data.table.rows[i][2],
            "Registered Voters": data.table.rows[i][13],
            'Male Voters': data.table.rows[i][14],
            'Female Voters': data.table.rows[i][15],
            'Urbanisation': data.table.rows[i][16],
            "Turnout": data.table.rows[i][4],
            "Population": data.table.rows[i][5],
            "Gender Blnce Men": data.table.rows[i][17],
            "Gender Blnce Women": data.table.rows[i][18],
            "Urban": data.table.rows[i][19],
            "Rural": data.table.rows[i][20],
            "WATER": data.table.rows[i][21],
            "ELECTRICITY": data.table.rows[i][22],
            "PAKKA": data.table.rows[i][23],
            "Employment": data.table.rows[i][24],
            "Major Clans": data.table.rows[i][25],
            "Winning Party": data.table.rows[i][6],
            "Winning Candidate": data.table.rows[i][7],
            "Votes Polled for Winner": data.table.rows[i][8],
            "Runnerup Party": data.table.rows[i][9],
            "Runnerup Candidate": data.table.rows[i][10],
            "Votes Polled for Runnerup": data.table.rows[i][11],
            "Color Code": data.table.rows[i][12]
        };
    };
}

function addcards() {
    var html = [];
    var tot = Object.keys(na_feeds).length;
    var ctr = 1;
    var bgclass = 'XXXX';
    html.push('<div class="row">');
    for (var i = 0 ; i <= (tot - 1) ; i++) {
        if (ctr == 4) {
            html.push('</div><div class="row">');
            ctr = 1;
        }
        if (na_feeds[i + 1]["Winning Party"] == "") {
            undecided.push(na_feeds[i + 1]["ID"]);
            bgclass = "XXXX";
        }
        else {
            bgclass = na_feeds[i + 1]["Winning Party"];
        }
        html.push('<div class="span4 ' + getPartyColor(bgclass) + '" id="na' + na_feeds[i + 1]["ID"] + '">');
        html.push('<div class="place-right gradient ' + bgclass + '" style="margin: 10px; min-height:48px; min-width:48px" id="flag' + na_feeds[i + 1]["ID"] + '"></div>');
        html.push('<h3 class="fg-color-white">&lt;NA-' + na_feeds[i + 1]["ID"] + '&gt;</h3>');
        html.push('<p class="fg-color-white">' + na_feeds[i + 1]["District"] + ' / ' + na_feeds[i + 1]["Winning Party"] + '</p>');
        html.push('</div>');
        updatewinningcount(na_feeds[i + 1]["Winning Party"]);
        ctr++;
    }
    html.push('</div>');
    $("#resultsgrid").append(html.join(''));
}
function recheck() {
    var reCheckArry = undecided;
    for (var i = 0; i < reCheckArry.length - 1; i++) {
        set_na_feedsIndiviual(reCheckArry[i]);
    }
}
function set_na_feedsIndiviual(ID) {
    if (!year) { return false; }

    var URL = "https://fusiontables.googleusercontent.com/fusiontables/api/query?sql=";


    var sSelect = "SELECT+*+FROM+" + TableID;
    var sWhere = "+where+Year=" + year + "+AND+ID=1+ORDER+BY+Year";
    var sLimit = "+LIMIT+500";
    var sSQLURL = URL + sSelect + sWhere + sLimit;
    var querytail = "&jsonCallback=?";
    var jqxhr = jQuery.get(sSQLURL + querytail, set_na_feedsIndiviual_handler, "jsonp");
}
function set_na_feedsIndiviual_handler(data) {
    var rowNo = data.table.rows.length;
    if (rowNo <= 0) { return false; }
    var tt = 1;
    for (var i = 0; i < rowNo; i++) {
        na_feeds_Indiviual[data.table.rows[i][0]] = {
            "ID": data.table.rows[i][0],
            "Year": data.table.rows[i][1],
            "District": data.table.rows[i][3],
            "National Assembly Name": data.table.rows[i][2],
            "Registered Voters": data.table.rows[i][13],
            'Male Voters': data.table.rows[i][14],
            'Female Voters': data.table.rows[i][15],
            'Urbanisation': data.table.rows[i][16],
            "Turnout": data.table.rows[i][4],
            "Population": data.table.rows[i][5],
            "Gender Blnce Men": data.table.rows[i][17],
            "Gender Blnce Women": data.table.rows[i][18],
            "Urban": data.table.rows[i][19],
            "Rural": data.table.rows[i][20],
            "WATER": data.table.rows[i][21],
            "ELECTRICITY": data.table.rows[i][22],
            "PAKKA": data.table.rows[i][23],
            "Employment": data.table.rows[i][24],
            "Major Clans": data.table.rows[i][25],
            "Winning Party": data.table.rows[i][6],
            "Winning Candidate": data.table.rows[i][7],
            "Votes Polled for Winner": data.table.rows[i][8],
            "Runnerup Party": data.table.rows[i][9],
            "Runnerup Candidate": data.table.rows[i][10],
            "Votes Polled for Runnerup": data.table.rows[i][11],
            "Color Code": data.table.rows[i][12]
        };
    };
    updateCards();
}
function updateCards() {
    if (na_feeds_Indiviual != null) {
        var tot = Object.keys(na_feeds_Indiviual).length;
        for (var i = 0 ; i <= (tot - 1) ; i++) {

            if (na_feeds_Indiviual[i + 1]["Winning Party"] != "") {
                var card = $("#na" + na_feeds_Indiviual[i + 1]["ID"]);
                var flag = $("#flag" + na_feeds_Indiviual[i + 1]["ID"]);
                $(card).removeClass(getPartyColor("XXXX"));
                $(card).addClass(getPartyColor(na_feeds_Indiviual[i + 1]["Winning Party"]));
                $(card).removeClass("XXXX");
                $(flag).addClass(na_feeds_Indiviual[i + 1]["Winning Party"]);
                updatewinningcount(na_feeds_Indiviual[i + 1]["Winning Party"]);

                var index = undecided.indexOf(na_feeds_Indiviual[i + 1]["ID"]);
                undecided.splice(index, 1);
            }
        }     
    }
}
function updatewinningcount(party) {
    if (party == "N/A")
        party = "NA";
    if (party == "PML(QA)")
        party = "PMLQ";
    if (party == "PML(FG)")
        party = "PMLF";
    if (party == "MQM(H)")
        party = "MQMH";
    if (party == "PML(N)")
        party = "PMLN";

    var count = $("#patryCount_" + party).text();
    $("#patryCount_" + party).text(parseInt(count) + 1);
}